package cheme.Application.actions;

import java.beans.PropertyChangeEvent;
import java.io.PrintStream;

import org.eclipse.jface.action.Action;
import org.eclipse.ui.IPartListener2;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.TextConsole;

import cheme.Application.ChemeApplicationConstants;


import cheme.ProcessModel.ConsoleModel;

public class ShowConsoleAction extends Action implements ActionFactory.IWorkbenchAction
{
	/**
     * The workbench window this action is registered with.
     */
    private IWorkbenchWindow workbenchWindow;
    
    public ShowConsoleAction(IWorkbenchWindow window)
    {
        //super(WorkbenchMessages.SaveAction_text, window); 
    	
    	super("");
        
    	if (window == null)
        {
            throw new IllegalArgumentException();
        }
    	
        workbenchWindow = window;
        
        //setText("Show console view");
        setText("Hide console view"); // PA changed this as console view is visible by default
        setToolTipText("Toggles the visibility of the console view.");
        setId("console"); //$NON-NLS-1$
        
//        window.getWorkbench().getHelpSystem().setHelp(this, IWorkbenchHelpContextIds.SAVE_ACTION);
        
        setImageDescriptor(ChemeApplicationConstants.DESC_NEW);
        setDisabledImageDescriptor(ChemeApplicationConstants.DESC_NEW);
        
        setActionDefinitionId("org.eclipse.ui.file.save"); //$NON-NLS-1$
    }
    
    IWorkbenchWindow getWorkbenchWindow()
    {
    	return workbenchWindow;
    }
    
    static IViewPart view = null;
    static MessageConsole messageConsole = null;
    static TextConsole textConsole = null;
    static PrintStream printStream = null;
    static boolean bHideView = false; // True means hide, False means show
    //PrintStream oldOut = null;
    
    IPartListener2 listener = new IPartListener2()
	{
		public void partClosed(IWorkbenchPartReference partRef)
		{
			
			setText("Show console view");
			
			// PA commented this
			// This was making the view null and re setting the print stream.
			// Due to this with every toggle a new view was created
			//view.getSite().getPage().removePartListener(this);
			//view = null;
			//System.setOut(oldOut);
		}

		public void partActivated(IWorkbenchPartReference partRef) {}
		public void partBroughtToTop(IWorkbenchPartReference partRef) {}
		public void partDeactivated(IWorkbenchPartReference partRef) {}
		public void partHidden(IWorkbenchPartReference partRef) {}
		public void partInputChanged(IWorkbenchPartReference partRef) 
		{
			System.out.println("handling Event");
		}
		public void partOpened(IWorkbenchPartReference partRef) {}
		public void partVisible(IWorkbenchPartReference partRef) {}
	};
    
	@Override
	public void run()
	{
		//try
		//{
			/*if(null == view)
			{
				if(null == messageConsole)
				{
					MessageConsole messageConsole = new MessageConsole("My Console", null);
					ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[] {messageConsole});
					printStream = new PrintStream(messageConsole.newMessageStream());
				}
				
				oldOut = System.out;
				System.setOut(printStream);
				
				view = getWorkbenchWindow().getActivePage().showView("org.eclipse.ui.console.ConsoleView");
				
				view.getSite().getPage().addPartListener(listener);
				
				setText("Hide console view");
			}*/
			if(bHideView)
			{
				getWorkbenchWindow().getActivePage().hideView(view);
				
				bHideView = false;		
				setText("Show console view");
			}
			else
			{					
				createView();
			}
		//}
		/*catch(PartInitException e)
		{
			e.printStackTrace();
		}*/
	}

	public void dispose()
	{
	}
	
	// This method will create the console view.
	// This code is moved from try block of run()
	// Now run is used just to toggle the view
	public void createView()
	{
		try
		{
			if(null == view)
			{
				if(null == messageConsole)
				{
					messageConsole = new MessageConsole("Feedback Messages", null);		
					
					ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[] {messageConsole});
					if (null == printStream)
					{
						printStream = new PrintStream(messageConsole.newMessageStream());
					}
				}
				
				//oldOut = System.out;
				System.setOut(printStream);
				
				view = getWorkbenchWindow().getActivePage().showView("org.eclipse.ui.console.ConsoleView");
				
				view.getSite().getPage().addPartListener(listener);				
				
			}
			else
			{
				view = getWorkbenchWindow().getActivePage().showView("org.eclipse.ui.console.ConsoleView");					
			}
			setText("Hide console view");
			bHideView = true; 
			
			//ConsoleModel.messageConsole = messageConsole;	
			System.out.print("Clean");
			messageConsole.clearConsole();
			
		}
		catch(PartInitException e)
		{
			e.printStackTrace();
		}
		
	}
	
	/*public static void clearMyConsole()
	{
		messageConsole.clearConsole();
	}*/
	
}